use dta/SMDLevel.dta, clear

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************

*********************** CREATE BLOCS **********************************************

foreach round in 1 2 {
gen vs`round'_LAB=vs`round'_s+vs`round'_nsa+vs`round'_nkp 
gen vs`round'_LIB=vs`round'_a+vs`round'_v_a+vs`round'_v 
gen vs`round'_CON=vs`round'_fv+vs`round'_h_fv+vs`round'_h 
gen vs`round'_OTH=1-vs`round'_LAB-vs`round'_LIB-vs`round'_CON
}

foreach bloc in LAB LIB CON OTH {
gen vsF_`bloc'=vs1_`bloc'
replace vsF_`bloc'=vs2_`bloc' if SECOND==1
}

*** generating leads and lags
xtset SMD year

foreach round in 1 2 F {
	foreach bloc in LAB LIB CON OTH {
	gen next_vs`round'_`bloc'=f3.vs`round'_`bloc'
	gen prev_vs`round'_`bloc'=l3.vs`round'_`bloc'
	gen diff`round'_`bloc'=vs`round'_`bloc'-l3.vs`round'_`bloc'
	}
}

keep year SMD PR *vs*LAB* *vs*LIB* *vs*CON* *vs*OTH* diff*
order year SMD PR vs1* vs2* vsF* next_vs1* next_vs2* next_vsF* prev_vs1* prev_vs2* prev_vsF*

**********************************************************************************
**********************************************************************************
**********************************************************************************

gen AdvantageLibCon=(vs1_LIB-vs1_CON)/(vs1_CON+vs1_LIB)
gen AdvantageLibLab=(vs1_LIB-vs1_LAB)/(vs1_LIB+vs1_LAB)
gen AdvantageLabCon=(vs1_LAB-vs1_CON)/(vs1_LAB+vs1_CON)

foreach var in AdvantageLibCon AdvantageLibLab AdvantageLabCon {
gen lag_`var'=l3.`var'
}

sort SMD
merge SMD using dta/StableSample

sum lag_Advantage* if year==1921 & sample==1
corr lag_Advantage* if year==1921 & sample==1

**********************************************************************************
**********************************************************************************
**********************************************************************************

gen SMD_label="."
****replace SMD_label="District A" if SMD==104 /* alternative example district */
replace SMD_label="District A" if SMD==1800
replace SMD_label="District B" if SMD==1802


**********************************************************************************
*********************MAIN FIGURE WITH LABELS *************************************
**********************************************************************************

	foreach advantage in LibCon {

			twoway (lfitci diff1_LIB lag_Advantage`advantage' if year==1921 & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_LIB lag_Advantage`advantage' if year==1921 & sample==1, msymbol(Oh) msize(large)) ///
			(scatter diff1_LIB lag_Advantage`advantage' if year==1921 & sample==1 & SMD_label!=".", msymbol(Oh) msize(large) mcolor(black) mlabel(SMD_label) mlabp(1) mlabc(black)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5))  ///
			ytitle("ΔVS LIB") xtitle("LIB advantage over CON") title("(1)") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/_LIB.gph, replace
			*graph export figures/_LIB.pdf, replace
			
			twoway (lfitci diff1_LAB lag_Advantage`advantage' if year==1921 & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_LAB lag_Advantage`advantage' if year==1921 & sample==1, msymbol(Oh) msize(large)) ///
			(scatter diff1_LAB lag_Advantage`advantage' if year==1921 & sample==1 & SMD_label!=".", msymbol(Oh) msize(large) mcolor(black) mlabel(SMD_label) mlabp(1) mlabc(black)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5))  ///
			ytitle("ΔVS LAB") xtitle("LIB advantage over CON") title("(2)") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/_LAB.gph, replace
			*graph export figures/_LAB.pdf, replace
			
			twoway (lfitci diff1_CON lag_Advantage`advantage' if year==1921 & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_CON lag_Advantage`advantage' if year==1921 & sample==1, msymbol(Oh) msize(large)) ///
			(scatter diff1_CON lag_Advantage`advantage' if year==1921 & sample==1 & SMD_label!=".", msymbol(Oh) msize(large) mcolor(black) mlabel(SMD_label) mlabp(1) mlabc(black)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5))  ///
			ytitle("ΔVS CON") xtitle("LIB advantage over CON") title("(3)") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/_CON.gph, replace
			*graph export figures/_CON.pdf, replace
			
			twoway (lfitci diff1_OTH lag_Advantage`advantage' if year==1921 & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_OTH lag_Advantage`advantage' if year==1921 & sample==1, msymbol(Oh) msize(large)) ///
			(scatter diff1_OTH lag_Advantage`advantage' if year==1921 & sample==1 & SMD_label!=".", msymbol(Oh) msize(large) mcolor(black) mlabel(SMD_label) mlabp(1) mlabc(black)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5))  ///
			ytitle("ΔVS OTH") xtitle("LIB advantage over CON") title("(4)") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/_OTH.gph, replace
			*graph export figures/_OTH.pdf, replace
			

	cd figures/gph
	graph combine _LIB.gph _LAB.gph _CON.gph _OTH.gph, xcommon ycommon plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono)
	graph export ../Figure1.pdf, replace
	cd ../../
	}

